package pictureType

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"hosptial-record/api/base"
	"hosptial-record/forms"
	"hosptial-record/global"
	"hosptial-record/global/response"
	"hosptial-record/model"
	"net/http"
)

// List 类型列表
func List(ctx *gin.Context) {
	var types []model.PictureType
	res := global.DB.Order("sort").Find(&types)
	if res.Error != nil {
		zap.S().Errorw("PictureType [List] 失败：", "msg", zap.Error(res.Error))
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg: "获取失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg:  "获取成功",
		Data: types,
	})
}

// Create 新增类型
func Create(ctx *gin.Context) {
	var form forms.PictureTypeForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	picType := model.PictureType{
		Name: form.Name,
		Sort: form.Sort,
	}
	res := global.DB.Create(&picType)
	if res.Error != nil {
		zap.S().Errorw("PictureType [Create] 失败:", "msg", zap.Error(res.Error))
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "创建失败",
			Data: res.Error,
		})
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "创建成功",
	})
}

// Update 更新类型
func Update(ctx *gin.Context) {
	var form forms.EditPictureTypeForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	// 是否存在
	if res := global.DB.First(&model.PictureType{}, form.ID); res.RowsAffected == 0 {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "要修改的类型不存在",
		})
		return
	}
	picType := model.PictureType{
		Name: form.Name,
		Sort: form.Sort,
	}
	picType.ID = form.ID
	res := global.DB.Updates(&picType)
	if res.Error != nil {
		zap.S().Errorw("PictureType [Update] 失败:", "msg", zap.Error(res.Error))
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "修改失败",
			Data: res.Error,
		})
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "修改成功",
	})
}

// Delete 删除类型
func Delete(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "id不合法",
		})
		return
	}
	// 是否存在
	if res := global.DB.First(&model.PictureType{}, id); res.RowsAffected == 0 {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "要删除的类型不存在",
		})
		return
	}
	if res := global.DB.Delete(&model.PictureType{}, id); res.Error != nil {
		zap.S().Errorw("PictureType [Delete] 失败：", "msg", zap.Error(res.Error))
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg: "删除失败",
		})
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "删除成功",
	})
}

// PatientPictures 获取患者的所有图片和类型绑定
func PatientPictures(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		ctx.JSON(http.StatusPaymentRequired, response.BaseResponse{
			Msg: "id不合法",
		})
		return
	}
	fmt.Println(id)
	// 通过id查询所有分类
	var types []model.PictureType
	if err := global.DB.
		Order("sort").Find(&types).Error; err != nil {
		zap.S().Errorw("PictureType [PatientPictures] 失败：", "msg", zap.Error(err))
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "查询失败",
			Data: err,
		})
		return
	}
	var pics []model.Picture
	// 查询用户已有的图片
	if err := global.DB.Where("patient_id = ?", id).Find(&pics).Error; err != nil {
		zap.S().Errorw("PictureType [PatientPictures] 失败：", "msg", zap.Error(err))
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "查询失败",
			Data: err,
		})
		return
	}
	res := make([]model.PictureTypePatientRes, len(types))
	for index, _ := range res {
		curType := types[index]
		res[index] = model.PictureTypePatientRes{
			BaseModel: curType.BaseModel,
			Name:      curType.Name,
			Sort:      curType.Sort,
		}
		// 将id匹配的图片放入pictures中
		for _, pic := range pics {
			if pic.TypeID == curType.ID {
				res[index].Pictures = append(res[index].Pictures, pic)
			}
		}
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg:  "获取成功",
		Data: res,
	})
}
